<?php


namespace app\controller\admin;

use app\common\model\RechargeRecordModel;
use app\util\ExcelHandle;
use app\util\ReturnCode;
use think\Response;

class RechargeRecord extends Base
{

    /**
     * @notes: 充值记录信息列表
     * @return Response
     * @throws \think\db\exception\DbException
     * @author: speed
     */
    public function index(): Response {
        $school_id = $this->request->get('school_id', $this->userInfo['school_id']);
        $getArr = input('get.');
        $getArr['school_id'] = $school_id;
        $listObj = RechargeRecordModel::getList($getArr);
        return $this->buildSuccess([
            'list'  => $listObj['data'],
            'count' => $listObj['total'],
        ]);
    }

    public function download(): Response  {
        $titles = [['school_name' => '学校名称'],['staff_name'=>'姓名'],['phone'=>'手机号'],['order_no'=>'订单编号'],['order_type_text'=>'充值类型'],
            ['money'=>'充值金额(元)'],['recharge_time' => '充值时间'],['notes' => '备注']];
        try {
            $school_id = $this->request->get('school_id');
            $getArr = input('get.');
            $getArr['school_id'] = $school_id;
            $listObj = RechargeRecordModel::getList($getArr);
            $exportData = [];
            $len = 1;
            foreach ($listObj['data'] as $datum) {
                $datum['create_time'] .= "\t";
                if (isset($datum['staff'])){
                    $datum['student_no'] = $datum['staff']['student_no'];
                    $datum['staff_name'] = $datum['staff']['name'];
                    $datum['phone'] = $datum['staff']['phone'];
                }
                $newItem = array_merge(['id' => $len],$datum);
                array_push($exportData, $newItem);
                $len++;
            }
            ExcelHandle::downLoadExcel('充值记录' . date('YmdHi'), $titles, $exportData);
        } catch (\think\exception\ValidateException $e) {
            return $this->buildFailed(ReturnCode::FILE_SAVE_ERROR, $e->getMessage());
        }
    }
}